/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

function init_controlsEsri() {
    require([
        /*Importação da funcionalidade de legenda*/
        "esri/dijit/BasemapGallery",
        "esri/dijit/OverviewMap",
        "esri/dijit/LocateButton",
        "esri/toolbars/navigation",
        "esri/dijit/Scalebar",
        "esri/dijit/Measurement",
        "dijit/layout/BorderContainer",
        "dijit/layout/ContentPane",
        "dojo/domReady!"
    ], function(BaseMapGallery, OverviewMap, LocateButton, Navigation,
    Scalebar, Measurement
           ) {

        /*Criação do basemapGallery para alterção de base layer no mapa*/
        var basemapGallery = new BaseMapGallery({
            showArcGISBasemaps: true,
            map: map_esri
        }, "basemapGallery");
        /*Inicia o base gallery*/
        basemapGallery.startup();
        /*Relaciona msg de error ao basemapGallery quando ocorrer error neste*/
        basemapGallery.on("error", function(msg) {
            console.log("basemap gallery error:  ", msg);
        });

        
        /*Criação do controle de overViewmap*/
        var overviewMap = new OverviewMap({
            map: map_esri},
            dojo.byId("overviewDiv"));
        /*Inicialização do controle*/
        overviewMap.startup();
        $("#overviewDiv").hide();

        /*Criação do controle de geolocalização*/
        var geoLocate = new LocateButton({
            map: map_esri
        }, "LocateButton");
        geoLocate.startup();

        /*Objeto de navegação para zoom etc*/
        navigationControl = new Navigation(map_esri);
        
        var scalebar = new Scalebar({
          map: map_esri,
          scalebarUnit: "dual"
        });
        
        //scalebar.hide();
        
          /*Configuração de proxy*/
        esri.config.defaults.io.proxyUrl = "/proxy";
        esri.config.defaults.io.alwaysUseProxy = false;

        //This service is for development and testing purposes only. We recommend that you create your own geometry service for use within your applications
        esri.config.defaults.geometryService = new esri.tasks.GeometryService("http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");
        /*Adiciona evento ao carregar o mapa para iniciar a função initToolbar*/
//        dojo.connect(map_esri, 'onLoad', function(map) {
//            meansureControl(map);
//        });

        var measurement = new Measurement({
            WKID:900913,
            map: map_esri
        }, dojo.byId("measurementDiv"));
        measurement.startup();
    
    });
    
}
/*Função de desenho*/
function drawFeatureControl() {
    require([
        /*Importação da funcionalidade de legenda*/
        "esri/Color",
        "esri/symbols/SimpleMarkerSymbol",
        "esri/symbols/CartographicLineSymbol",
        "esri/symbols/PictureFillSymbol",
        "esri/symbols/SimpleLineSymbol",
        "esri/toolbars/draw",
        "esri/graphic",
        "dojo/domReady!"
    ], function(
            Color,
            SimpleMarkerSymbol, CartographicLineSymbol,
            PictureFillSymbol, SimpleLineSymbol, Draw, Graphic

            ) {

        //=====================Draw==========================
        /*Importações necessárias para funcionalidade de desenho*/

        /*Adiciona evento ao carregar o mapa. Carrega a função drawControl*/
        map_esri.on("load", createDrawControl);

        // markerSymbol is used for point and multipoint, see http://raphaeljs.com/icons/#talkq for more examples
        var markerSymbol = new SimpleMarkerSymbol();
        markerSymbol.setPath("M16,4.938c-7.732,0-14,4.701-14,10.5c0,1.981,0.741,3.833,2.016,5.414L2,25.272l5.613-1.44c2.339,1.316,5.237,2.106,8.387,2.106c7.732,0,14-4.701,14-10.5S23.732,4.938,16,4.938zM16.868,21.375h-1.969v-1.889h1.969V21.375zM16.772,18.094h-1.777l-0.176-8.083h2.113L16.772,18.094z");
        markerSymbol.setColor(new Color("#00FFFF"));

        // lineSymbol used for freehand polyline, polyline and line. 
        var lineSymbol = new CartographicLineSymbol(
                CartographicLineSymbol.STYLE_SOLID,
                new Color([255, 0, 0]), 3,
                CartographicLineSymbol.CAP_ROUND,
                CartographicLineSymbol.JOIN_MITER, 5
                );

        // fill symbol used for extent, polygon and freehand polygon, use a picture fill symbol
        // the images folder contains additional fill images, other options: sand.png, swamp.png or stiple.png
        var fillSymbol = new PictureFillSymbol(
                new SimpleLineSymbol(
                        SimpleLineSymbol.STYLE_SOLID,
                        new Color('#000'),
                        1
                        ),
                42,
                42
                );


        function addGraphic(evt) {
            //deactivate the toolbar and clear existing graphics 
            tb.deactivate();
            map_esri.enableMapNavigation();

            // figure out which symbol to use
            var symbol;
            if (evt.geometry.type === "point" || evt.geometry.type === "multipoint") {
                symbol = markerSymbol;
            } else if (evt.geometry.type === "line" || evt.geometry.type === "polyline") {
                symbol = lineSymbol;
            }
            else {
                symbol = fillSymbol;
            }
            map_esri.graphics.add(new Graphic(evt.geometry, symbol));
        }

        /*Função que cria o controle de desenho*/
        function createDrawControl() {
            tb = new Draw(map_esri);
            tb.on("draw-end", addGraphic);
        }
    });
}

/*Ativa ou desativa layer, recebe layer como parametro*/
function toogleLayer(layer) {
    if(layer.visible) {
        layer.setVisibility(false);
    } else {
        layer.setVisibility(true);
    }
}

/*Função de alternancia do controle historico de zoom*/
function historyNavigationContol(element) {
    var value =$(element).attr("value");
    
    if(value == "zoomNext") {
        navigationControl.zoomToNextExtent();
    } else if(value =="zoomPrevius") {
         navigationControl.zoomToPrevExtent();
    } 
}

/*Oculta todas as divs dos controles*/
function hideAllControls() {
    $("#menu_esri").hide();
    $("#overviewDiv2").hide();
    $("#map_esri").hide();
    $("#btn_escolherBaseMap").hide();
    $("#scale").hide();
    $("#measureWindow").hide();
    $("#zoomHistorico").hide();
    $("#info").hide();
}